package com.frame.easy.modular.activiti.controller;

import com.frame.easy.base.controller.BaseController;
import com.frame.easy.modular.activiti.model.Task;
import com.frame.easy.modular.activiti.service.TaskService;
import com.frame.easy.result.Tips;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 待办任务
 *
 * @author tengchong
 * @date 2020/3/26
 */
@Controller
@RequestMapping("/auth/activiti/task")
public class TaskController extends BaseController {

    /**
     * view 路径
     */
    private static final String PREFIX = "modular/activiti/task/";

    @Autowired
    private TaskService service;

    /**
     * 列表
     * @param status 任务状态
     *
     * @return view
     */
    @RequestMapping("list/{status}")
    public String list(Model model, @PathVariable("status") String status) {
        logger.debug("/auth/activiti/task/list/" + status);
        model.addAttribute("status", status);
        return PREFIX + "list";
    }

    /**
     * 查询数据
     *
     * @param status 任务状态
     * @param task 查询条件
     * @return 数据
     */
    @RequestMapping("select/{status}")
    @ResponseBody
    public Tips select(@PathVariable("status") String status, @RequestBody(required = false) Task task) {
        logger.debug("/auth/activiti/task/select/" + status);
        task.setStatus(status);
        return Tips.getSuccessTips(service.select(task));
    }

    /**
     * 签收任务
     *
     * @param taskId 任务id
     * @return Tips
     */
    @RequestMapping("claim/task/{taskId}")
    @ResponseBody
    public Tips claimTask(@PathVariable("taskId") String taskId) {
        logger.debug("/auth/activiti/claim/task/" + taskId);
        service.claimTask(taskId);
        return Tips.getSuccessTips();
    }

    /**
     * 任务处理页面
     *
     * @param model  model
     * @param taskId 任务Id
     * @return view
     */
    @RequestMapping("task/form/{taskId}")
    public String taskForm(Model model, @PathVariable("taskId") String taskId) {
        logger.debug("/auth/activiti/task/form/" + taskId);
        service.readTaskForm(taskId, model);
        return PREFIX + "task-form";
    }

    /**
     * 流转任务
     *
     * @param taskId  任务Id
     * @param request request
     * @return Tips
     */
    @RequestMapping("complete/task/{taskId}")
    @ResponseBody
    public Tips completeTask(@PathVariable("taskId") String taskId, HttpServletRequest request) {
        logger.debug("/auth/activiti/complete/task/" + taskId);
        service.completeTask(taskId, request);
        return Tips.getSuccessTips();
    }

    /**
     * 撤销申请
     *
     * @param processInstanceId 流程实例ID
     * @param deleteReason      撤销原因(可为空)
     *
     */
    @RequestMapping("revoke/{processInstanceId}")
    @ResponseBody
    public Tips revoke(@PathVariable("processInstanceId") String processInstanceId,
                       @RequestParam(value = "deleteReason", required = false) String deleteReason) {
        logger.debug("/auth/activiti/revoke/" + processInstanceId);
        service.revoke(processInstanceId, deleteReason);
        return Tips.getSuccessTips();
    }
}
